unit uUtils;

interface

uses Oracle, Jpeg, Graphics, pngimage, SysUtils, Grids, gnugettext, StrUtils, Windows, Classes, variants, DBTables;

var
  CanSelect: boolean;
  pct_Temp: TPicture;
  resizeBmpImg: Graphics.TBitmap;
  resizeJpgImg: TJPEGimage;
function BToStr(boolvalue: boolean): string;
function StrToB(strvalue: variant): boolean;
function GetImage(Query: string; OraSession: TOracleSession; GetSmallImage: boolean = false): TPicture;
function BMPtoJPG(var Bitmap: Graphics.TBitmap; JPGpicFileName: string): boolean;
function GetFormatedStringDateNOW(OraSession: TOracleSession): string;
function GetCurrencySymbol(OraSession: TOracleSession): string;
function SumColGridData(Grid: TStringGrid; ACol, fromRow, toRow: integer): string;
function GetAppVersion(FileName: string): string;
function resizeImage(sInImage: TJPEGimage; ImWidth: integer = 320; ImHeight: integer = 240): TJPEGimage;
procedure SetWindowTransparency(WHandle: hWnd; Amount: integer); // By Paul Maravelias
function IsStrANumber(const S: string): boolean;
procedure SocketTextParse(SocketText: string; var EventType, TournamentID, TableID: string);
procedure DrawTransparentBmp(Cnv: Graphics.TCanvas; x, y: integer; Bmp: Graphics.TBitmap; clTransparent: TColor);
function PrintWindow(aHwnd: HWND; aBitmap: Graphics.TBitmap): Boolean;

const
  EventTypeTicket: string = 'TICKET';
  EventTypeFromTable: string = 'FROM_TABLE';
  EventTypeInspectorChange: string = 'INSPECTOR_CHANGE';
  EventTypeCallIns: string = 'INSPECTOR_CALL';
  EventTypeBreakTable: string = 'BREAK_TABLE';
  EventTypeCashierChange: string = 'CASHIER_CHANGE';
  EventTypeBarmanChange: string = 'BARMAN_CHANGE';
  // -----
  LoginForm_Table: string = 'Table';
  LoginForm_Hall: string = 'Hall';
  LoginForm_Bar: string = 'Bar';
  LoginForm_Cage: string = 'Cage';
  // ------
  TicketTypeRebuy: string = 'REBUY';
  TicketTypeBuy: string = 'BUY';
  TicketTypeAddon: string = 'ADDON';
  EventEndDelay: string = '0.0001';
  DeleteOperation: string = 'DELETE';
  InsertOperation: string = 'INSERT';
  UpdateOperation: string = 'UPDATE';
  PlayerOnTable: string = 'ON_TABLE';
  PlayerOutFromTable: string = 'OUT';
  PlayerWithTicket: string = 'HAVE_TICKET';
  PlayerIsFinalist: string = 'FINALIST';
  PlayerIsWinner: string = 'WINNER';
  QueryPlayersTicketsWinsTournaments: WideString =
    'select PL.ID, PL.NAME, PL.SURNAME, PL.BIRTH_DATE, PL.STREET, PL.CITY, PL.COUNTRY_ID, TI.FEE, TI.RAKE,tou.name as tournament,TI.buy_in, TIW.WIN_VALUE, TIW.WIN_CLASSIFICATION from players pl' +
    ' join tickets ti on ti.PLAYER_ID=pl.id left join ticket_win tiw on tiw.TICKET_ID = ti.id join tournaments tou on tou.id=ti.TOURNAMENT_ID';

type
  TGridHack = class(TCustomGrid);

implementation

procedure DrawTransparentBmp(Cnv: Graphics.TCanvas; x, y: integer; Bmp: Graphics.TBitmap; clTransparent: TColor);
var
  bmpXOR, bmpAND, bmpINVAND, bmpTarget: Graphics.TBitmap;
  oldcol: Longint;
begin
  try
    bmpAND := Graphics.TBitmap.Create;
    bmpAND.Width := Bmp.Width;
    bmpAND.Height := Bmp.Height;
    bmpAND.Monochrome := True;
    oldcol := SetBkColor(Bmp.Canvas.Handle, ColorToRGB(clTransparent));
    BitBlt(bmpAND.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, Bmp.Canvas.Handle, 0, 0, SRCCOPY);
    SetBkColor(Bmp.Canvas.Handle, oldcol);

    bmpINVAND := Graphics.TBitmap.Create;
    bmpINVAND.Width := Bmp.Width;
    bmpINVAND.Height := Bmp.Height;
    bmpINVAND.Monochrome := True;
    BitBlt(bmpINVAND.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, bmpAND.Canvas.Handle, 0, 0, NOTSRCCOPY);

    bmpXOR := Graphics.TBitmap.Create;
    bmpXOR.Width := Bmp.Width;
    bmpXOR.Height := Bmp.Height;
    BitBlt(bmpXOR.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, Bmp.Canvas.Handle, 0, 0, SRCCOPY);
    BitBlt(bmpXOR.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, bmpINVAND.Canvas.Handle, 0, 0, SRCAND);

    bmpTarget := Graphics.TBitmap.Create;
    bmpTarget.Width := Bmp.Width;
    bmpTarget.Height := Bmp.Height;
    BitBlt(bmpTarget.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, Cnv.Handle, x, y, SRCCOPY);
    BitBlt(bmpTarget.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, bmpAND.Canvas.Handle, 0, 0, SRCAND);
    BitBlt(bmpTarget.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, bmpXOR.Canvas.Handle, 0, 0, SRCINVERT);
    BitBlt(Cnv.Handle, x, y, Bmp.Width, Bmp.Height, bmpTarget.Canvas.Handle, 0, 0, SRCCOPY);
  finally
    bmpXOR.Free;
    bmpAND.Free;
    bmpINVAND.Free;
    bmpTarget.Free;
  end;
end;

function resizeImage(sInImage: TJPEGimage; ImWidth, ImHeight: integer): TJPEGimage;
var
  Rectangle: TRect;
  iWidth, iHeight: integer;
begin
  try
    iWidth := 320;
    iHeight := 240;
    resizeJpgImg := TJPEGimage.Create;
    resizeBmpImg := Graphics.TBitmap.Create;
    resizeJpgImg.Assign(sInImage);
    with resizeBmpImg do
    begin
      if resizeJpgImg.Width > resizeJpgImg.Height then
      begin
        Width := iWidth;
        Height := iHeight;
        Rectangle := Rect(0, 0, iWidth, iHeight);
      end
      else
      begin
        Width := iHeight;
        Height := iWidth;
        Rectangle := Rect(0, 0, iHeight, iWidth);
      end;
      Canvas.StretchDraw(Rectangle, resizeJpgImg);
    end;
  finally
    resizeJpgImg.Assign(resizeBmpImg);
    result := resizeJpgImg;
  end;
end;

function StrToB(strvalue: variant): boolean;
begin
  if (strvalue = 0) or (strvalue = null) then
    result := false
  else if strvalue = 1 then
    result := True;
end;

function BToStr(boolvalue: boolean): string;
begin
  if boolvalue then
    result := '1'
  else if not boolvalue then
    result := '0';
end;

function SumColGridData(Grid: TStringGrid; ACol, fromRow, toRow: integer): string;
var
  i: integer;
  SumValue: double;
begin
  SumValue := 0;
  for i := fromRow to toRow - 1 do
  begin
    if (Grid.Cells[ACol, i] <> '') then
    begin
      if IsStrANumber(Grid.Cells[ACol, i]) then
        SumValue := SumValue + strtofloat(Grid.Cells[ACol, i]);
    end;
  end;
  result := '(' + FloatToStr(SumValue) + ')';
end;

function IsStrANumber(const S: string): boolean;
var
  P: PChar;
begin
  P := PChar(S);
  result := True;
  while P^ <> #0 do
  begin
    if not(P^ in ['0' .. '9', '.', ',']) then
      result := false;
    Inc(P);
  end;
end;

function GetAppVersion(FileName: string): string;
var
  Size, Size2: DWORD;
  Pt, Pt2: Pointer;
  dwWnd: DWORD;
begin
  Size := getFileVersionInfoSize(PChar(FileName), dwWnd);
  if Size > 0 then
  begin
    GetMem(Pt, Size);
    try
      GetFileVersionInfo(PChar(FileName), 0, Size, Pt);
      VerQueryValue(Pt, '\', Pt2, Size2);
      with TVSFixedFileInfo(Pt2^) do
      begin
        result := IntToStr(HiWord(dwFileVersionMS)) + '.' + IntToStr(LoWord(dwFileVersionMS)) + '.' + IntToStr(HiWord(dwFileVersionLS)) + '.' + IntToStr(LoWord(dwFileVersionLS));
      end;
    finally
      FreeMem(Pt);
    end;
  end;
end;

function GetFormatedStringDateNOW(OraSession: TOracleSession): string;
var
  OracleQuery: TOracleQuery;
begin
  OracleQuery := TOracleQuery.Create(nil);
  OracleQuery.Session := OraSession;
  OracleQuery.SQL.Clear;
  OracleQuery.SQL.Text := ('select sysdate from dual');
  OracleQuery.Execute;
  result := FormatDateTime('dd.mm.yyyy hh:nn:ss', OracleQuery.FieldAsDate(0));
  OracleQuery.Close;
  freeandnil(OracleQuery);
end;

function GetCurrencySymbol(OraSession: TOracleSession): string;
var
  OracleQuery: TOracleQuery;
begin
  OracleQuery := TOracleQuery.Create(nil);
  OracleQuery.Session := OraSession;
  OracleQuery.SQL.Clear;
  OracleQuery.SQL.Text := ('select currency_symbol from settings');
  OracleQuery.Execute;
  result := OracleQuery.Field(0);
  OracleQuery.Close;
  freeandnil(OracleQuery);
end;

procedure SocketTextParse(SocketText: string; var EventType, TournamentID, TableID: string);
begin
  EventType := LeftStr(SocketText, pos('|1|', SocketText) - 1);
  TournamentID := MidStr(SocketText, pos('|1|', SocketText) + 3, (pos('|2|', SocketText) - pos('|1|', SocketText)) - 3);
  TableID := RightStr(SocketText, Length(SocketText) - pos('|2|', SocketText) - 2);
end;

function GetImage(Query: string; OraSession: TOracleSession; GetSmallImage: boolean): TPicture;
var
  ABlob: TLOBLocator;
  JPEGImage: TJPEGimage;
  pngimage: TPngImage;
  BMPImage: Graphics.TBitmap;
  OracleQuery: TOracleQuery;
begin
  result := nil;
  OracleQuery := TOracleQuery.Create(nil);
  OracleQuery.SQL.Text := (Query);
  OracleQuery.Session := OraSession;
  OracleQuery.Execute;
  if OracleQuery.RowCount > 0 then
  begin
    if GetSmallImage then
    begin
      ABlob := OracleQuery.LOBField('IMAGE_SMALL');
    end
    else
    begin
      ABlob := OracleQuery.LOBField('IMAGE');
    end;
    ABlob.Position := 0;
    if OracleQuery.Field('IMAGE_TYPE') = 'PNG' then
    begin
      pngimage := TPngImage.Create;
      pngimage.LoadFromStream(ABlob);
      if ABlob.Size > 0 then
      begin
        pct_Temp.Bitmap.Assign(pngimage);
        result := pct_Temp;
      end;
      freeandnil(pngimage);
    end
    else if OracleQuery.Field('IMAGE_TYPE') = 'BMP' then
    begin
      BMPImage := Graphics.TBitmap.Create;
      BMPImage.LoadFromStream(ABlob);
      if ABlob.Size > 0 then
      begin
        pct_Temp.Bitmap.Assign(BMPImage);
        result := pct_Temp;
      end;
      freeandnil(BMPImage);
    end
    else if (OracleQuery.Field('IMAGE_TYPE') = 'JPG') or (OracleQuery.Field('IMAGE_TYPE') = 'JPEG') then
    begin
      JPEGImage := TJPEGimage.Create;
      JPEGImage.LoadFromStream(ABlob);
      if ABlob.Size > 0 then
      begin
        pct_Temp.Bitmap.Assign(JPEGImage);
        result := pct_Temp;
      end;
      freeandnil(JPEGImage);
    end;
  end;
  OracleQuery.Close;
  freeandnil(OracleQuery);
end;

function BMPtoJPG(var Bitmap: Graphics.TBitmap; JPGpicFileName: string): boolean;
var
  JpegImg: TJPEGimage;
begin
  result := false;
  try
    JpegImg := TJPEGimage.Create;
    try
      JpegImg.Assign(Bitmap);
      JpegImg.SaveToFile(JPGpicFileName);
      result := True;
    finally
      JpegImg.Free
    end;
  finally
    Bitmap.Free
  end;
end;

function PrintWindow(aHwnd: HWND; aBitmap: Graphics.TBitmap): Boolean;
var
  rc: TRect;
  huser32: THandle;
  F: function(Hnd: HWND; HdcBlt: HDC; nFlags: UINT): BOOL; stdcall;
begin
  Result := False;
  huser32 := GetModuleHandle(user32);
  if huser32 <> 0 then begin
    @F := GetProcAddress(huser32, 'PrintWindow');
    if @F <> nil then begin
      GetWindowRect(aHwnd, rc);
      aBitmap.Width := rc.Right - rc.Left;
      aBitmap.Height := rc.Bottom - rc.Top;
      aBitmap.Canvas.Lock;
      try
        Result := F(aHWnd, aBitmap.Canvas.Handle, 0);
      finally
        aBitmap.Canvas.Unlock;
      end;
    end;
  end;
end;

procedure SetWindowTransparency(WHandle: hWnd; Amount: integer); // By Paul Maravelias
begin
  SetWindowLong(WHandle, GWL_EXSTYLE, GetWindowLong(WHandle, GWL_EXSTYLE) or WS_EX_LAYERED);
  SetLayeredWindowAttributes(WHandle, 0, (255 * Amount) div 100, LWA_ALPHA);
end;

end.
